package cn.ffcs.bpmpvc.services.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import cn.ffcs.bpm.model.Message;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.Report;
import cn.ffcs.bpmpvc.dao.AppBusinessReqDao;
import cn.ffcs.bpmpvc.dao.FlowPvcDao;
import cn.ffcs.bpmpvc.dao.MessageDao;
import cn.ffcs.bpmpvc.services.FlowPvcMessageService;
import cn.ffcs.bpmpvc.web.forms.FlowTaskForm;
import cn.ffcs.bpmpvc.web.views.FlowTaskObjView;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.organ.dao.IPersonDAO;
import cn.ffcs.organ.dao.LinkMethodDAO;
import cn.ffcs.organ.model.LinkMethod;
import cn.ffcs.organ.model.Person;
import cn.ffcs.util.converter.DateUtil;

public class FlowPvcMessageServiceImpl implements FlowPvcMessageService {
	private static final Logger logger=Logger.getLogger(FlowPvcMessageServiceImpl.class);
	private FlowPvcDao flowPvcDao;
	private MessageDao messageDao;
	private AppBusinessReqDao appBusinessReqDao;
	private IPersonDAO<Person> personDAO;
	private LinkMethodDAO<LinkMethod> linkMethodDAO;

	public LinkMethodDAO<LinkMethod> getLinkMethodDAO() {
		return linkMethodDAO;
	}

	public void setLinkMethodDAO(LinkMethodDAO<LinkMethod> linkMethodDAO) {
		this.linkMethodDAO = linkMethodDAO;
	}

	public IPersonDAO<Person> getPersonDAO() {
		return personDAO;
	}

	public void setPersonDAO(IPersonDAO<Person> personDAO) {
		this.personDAO = personDAO;
	}

	public AppBusinessReqDao getAppBusinessReqDao() {
		return appBusinessReqDao;
	}

	public void setAppBusinessReqDao(AppBusinessReqDao appBusinessReqDao) {
		this.appBusinessReqDao = appBusinessReqDao;
	}

	@SuppressWarnings("unchecked")
	public void sendTaskProcessMessage() {
		// TODO Auto-generated method stub
		
		logger.info("工作汇报提醒执行中start>>>>>>>>");
		FlowTaskForm flowTaskForm=new FlowTaskForm();
		flowTaskForm.setState("active");
		List<FlowTaskObjView> list=getAppBusinessReqDao().queryPageTaskList(flowTaskForm, 1, 100).getDataList();
		if(list==null)return;
		for (int i = 0; i < list.size(); i++) {
			FlowTaskObjView flowTaskObjView=list.get(i);
			Message message=new Message();
			LinkMethod linkMethod=getLinkMethodDAO().findByIdAndType(flowTaskObjView.getUserId(), "手机");
			if(linkMethod==null){
				continue;
			}
			message.setTelNumber(linkMethod.getValue());				
			message.setBegin(new Date());
			message.setEnd(DateUtil.getMaxDate());
			message.setName(flowTaskObjView.getUserName());
			message.setContent("工作汇报提醒："+flowTaskObjView.getTitle());
			message.setType("2");
			getAppBusinessReqDao().save(message);
		}
		logger.info("工作汇报提醒执行中end>>>>>>>>");
	}

	@SuppressWarnings("unchecked")
	public void sendTaskMessage() {
		// TODO Auto-generated method stub
		logger.info("待办任务提醒执行中start>>>>>>>>");
		FlowTaskForm flowTaskForm=new FlowTaskForm();
		flowTaskForm.setState("active");
		List<FlowTaskObjView> list=getAppBusinessReqDao().queryPageTaskList(flowTaskForm, 1, 100).getDataList();
		if(list==null)return;
		for (int i = 0; i < list.size(); i++) {
			FlowTaskObjView flowTaskObjView=list.get(i);
			if(!"1".equals(flowTaskObjView.getIsSend())){
				cn.ffcs.bpm.model.ProcTranRecord procTranRecord=new ProcTranRecord();
				procTranRecord=getFlowPvcDao().getTask(Integer.valueOf(flowTaskObjView.getFlowTaskId()));
				procTranRecord.setIsSend("1");
				getFlowPvcDao().save(procTranRecord);
				Message message=new Message();
//				Person person=getPersonDAO().findById(Integer.valueOf(flowTaskObjView.getUserId()));
//				List<LinkMethod> list2=person.getLinkMethods();
				
				LinkMethod linkMethod=getLinkMethodDAO().findByIdAndType(flowTaskObjView.getUserId(), "手机");
				if(linkMethod==null){
					continue;
				}
				message.setTelNumber(linkMethod.getValue());				
				message.setBegin(new Date());
				message.setEnd(DateUtil.getMaxDate());
				message.setName(flowTaskObjView.getUserName());
				message.setContent("待办任务提醒："+flowTaskObjView.getTitle());
				message.setType("1");
				getAppBusinessReqDao().save(message);
			}			
		}

		logger.info("待办任务提醒执行中end>>>>>>>>");
	}
	@SuppressWarnings("deprecation")
	public void monthReportTask() {
		// TODO Auto-generated method stub
		logger.info("月报待办处理start>>>>>>>>");
		Date date=new Date();
		int monthNum=date.getMonth()+1;
		System.out.println(monthNum);
		List<BusinessRequest> list=getAppBusinessReqDao().findWeekSetBsReq("month");
		System.out.println(list.size());
		List<Report> reportList=getAppBusinessReqDao().findNowOverReport("02");
		System.out.println(reportList.size());
		BusinessRequest businessRequest=null;
		Report report=null;
		boolean bool=false;
		for (int i = 0; i < list.size(); i++) {
			businessRequest=list.get(i);
			bool=false;
			for (int j = 0; j < reportList.size(); j++) {
				report=reportList.get(j);
				if(report.getReqId().equals(businessRequest.getId().toString())){
					bool=true;
					break;
				}
			}
			if(bool==false){
				Report reportNew=new Report();
				reportNew.setPersonId(businessRequest.getProdPerson().getId().toString());
				reportNew.setType("02");
				reportNew.setReqId(businessRequest.getId().toString());
				reportNew.setDwNum(String.valueOf(monthNum));
				reportNew.setReceiveTime(new Date());
				getAppBusinessReqDao().save(reportNew);
			}
		}
		logger.info("月报待办处理end>>>>>>>>");
	}
	public static void main(String[] args) {
		Date date=new Date();
		System.out.println(date.getMonth());
	}
	public void weekReportTask() {
		// TODO Auto-generated method stub
		logger.info("周报待办处理start>>>>>>>>");
		int weekNum=getAppBusinessReqDao().weekNum();
		System.out.println(weekNum);
		List<BusinessRequest> list=getAppBusinessReqDao().findWeekSetBsReq("week");
		System.out.println(list.size());
		List<Report> reportList=getAppBusinessReqDao().findNowOverReport("01");
		System.out.println(reportList.size());
		BusinessRequest businessRequest=null;
		Report report=null;
		boolean bool=false;
		for (int i = 0; i < list.size(); i++) {
			businessRequest=list.get(i);
			bool=false;
			for (int j = 0; j < reportList.size(); j++) {
				report=reportList.get(j);
				if(report.getReqId().equals(businessRequest.getId().toString())){
					bool=true;
					break;
				}
			}
			if(bool==false){
				Report reportNew=new Report();
				reportNew.setPersonId(businessRequest.getProdPerson().getId().toString());
				reportNew.setType("01");
				reportNew.setReqId(businessRequest.getId().toString());
				reportNew.setDwNum(String.valueOf(weekNum));
				reportNew.setReceiveTime(new Date());
				getAppBusinessReqDao().save(reportNew);
			}
		}
		logger.info("周报待办处理end>>>>>>>>");
	}
	public FlowPvcDao getFlowPvcDao() {
		return flowPvcDao;
	}

	public void setFlowPvcDao(FlowPvcDao flowPvcDao) {
		this.flowPvcDao = flowPvcDao;
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}


}
